<style type="text/css">
	.border-container::after {
		content: "Editar pedido {{ $order->order_number }} - {{{ $customer->name }}}";
	}
</style>
<div class="container">
	{{ Form::model($order, array('route' => array('order.update', $order->id), 'method' => 'PUT', 'class' => 'form-horizontal', 'role' => 'form')) }}
		<div class="border-container">
			@if (!$errors->isEmpty())
			<div class="alert alert-danger">
				<strong>Ocorreram os seguintes erros:</strong>
				{{ HTML::ul($errors->all()) }}
			</div>
			@endif
			
			<ul class="nav nav-tabs">
				<li class="active"><a href="#identification" data-toggle="tab">Identificação</a></li>
	            <li><a href="#components" data-toggle="tab">Componentes</a></li>
			</ul>
			
			<div class="tab-content">
            	<div class="tab-pane active" id="identification">
					<div class="form-group">
						<label for="order_number" class="col-sm-2 control-label">Nro Pedido</label>
						<div class="col-sm-4">
		                	{{ Form::text('order_number', null, array('class'=>'form-control', 'maxlength' => '15', 'autocomplete' => 'off')) }}
						</div>
            		</div>
            		
            		<div class="form-group">
						<label for="customer_id" class="col-sm-2 control-label">Cliente</label>
						<div class="col-sm-9">
		                	{{ Form::select('customer_id', $customers, $order->costumer_id, array('class'=>'form-control', 'id' => 'customerId')) }}
						</div>
            		</div>
            		
            		<div class="form-group">
						<label for="production_number" class="col-sm-2 control-label">Ordem de Produção</label>
						<div class="col-sm-4">
		                	{{ Form::text('production_number', null, array('class'=>'form-control', 'maxlength' => '9', 'autocomplete' => 'off')) }}
						</div>
            		</div>
            		
            		<div class="form-group">
						<label for="amount" class="col-sm-2 control-label">Qtd a Produzir</label>
						<div class="col-sm-4">
		                	{{ Form::text('amount', null, array('class'=>'form-control', 'maxlength' => 9, 'id' => 'amount', 'autocomplete' => 'off')) }}
						</div>
            		</div>
            		
            		<div class="form-group">
						<label for="order_date" class="col-sm-2 control-label">Data de Entrada</label>
						<div class="col-sm-4">
							<div class="input-group date">
			                	{{ Form::text('order_date', $order->order_date->format('d/m/Y'), array('class'=>'form-control', 'id' => 'orderDate', 'autocomplete' => 'off')) }}
			                	<span class="input-group-addon"><i class="glyphicon glyphicon glyphicon-calendar"></i></span>
			                </div>
						</div>
            		</div>
            		
            		<div class="form-group">
						<label for="production_end_date" class="col-sm-2 control-label">Data de Entrega</label>
						<div class="col-sm-4">
							<div class="input-group date">
			                	{{ Form::text('production_end_date', $order->production_end_date->format('d/m/Y'), array('class'=>'form-control', 'id' => 'productionEndDate', 'autocomplete' => 'off')) }}
			                	<span class="input-group-addon"><i class="glyphicon glyphicon glyphicon-calendar"></i></span>
			                </div>
						</div>
            		</div>
            		
            		<div class="form-group">
						<label for="production_start_date" class="col-sm-2 control-label">Início da Produção</label>
						<div class="col-sm-4">
							@if ($canEditProductionStart)
								<div class="input-group date">
			                		{{ Form::text('production_start_date', $order->production_start_date->format('d/m/Y'), array('class'=>'form-control', 'id' => 'productionStartDate', 'autocomplete' => 'off')) }}
			                @else
			                	<div class="input-group">
			                	{{-- não existe um atributo false para o campo disabled. TODO: encontrar uma forma melhor de fazer isso sem repetir todo o código --}}
			                	{{ Form::text('production_start_date', $order->production_start_date->format('d/m/Y'), array('class'=>'form-control', 'id' => 'productionStartDate', 'disabled' => 'false')) }}
			                @endif
			                	<span class="input-group-addon"><i class="glyphicon glyphicon glyphicon-calendar"></i></span>
			                </div>
						</div>
            		</div>
            	</div>
            	
            	<div class="tab-pane" id="components">
            	
		            <div id="componentsHeader" class="form-group">
		            	<div class="col-sm-2 centered">
		            		<label>Tipo</label>
		            	</div>
		            	<div class="col-sm-2 centered">
		            		<label>Código do CIGAM</label>
		            	</div>
		            	<div class="col-sm-1 centered">
		            		<label>Qtd</label>
		            	</div>
		            	<div class="col-sm-4">
		            		<label>Nome do Componente</label>
		            	</div>
		            	<div class="col-sm-3">
		            		<label>Ações</label>
		            	</div>
		            </div>
            	
            		@foreach($orderComponents as $orderComponent)
						<div class="form-group component-group current-component">
							<div class="col-sm-2" style="padding-right: 3px;">
								{{ Form::select('disabledAccessories[' . $orderComponent->component->id . ']', array('0'=>'Operação','1'=>'Insumo'),  $orderComponent->component->accessory, array('class'=>'form-control', 'disabled' => 'disabled')) }}
							</div>
							<div class="col-sm-2 col-component">
								{{ Form::text('disabledCigamCodes[' . $orderComponent->component->id . ']', $orderComponent->component->code, array('class'=>'form-control', 'disabled' => 'disabled')) }}
							</div>
							<div class="col-sm-1 col-component">
								{{ Form::text('disabledAmounts[' . $orderComponent->component->id . ']', $orderComponent->amount, array('class'=>'form-control', 'disabled' => 'disabled')) }}
				  			</div>
							<div class="col-sm-4 col-component">
				  				{{ Form::text('disabledComponents[' . $orderComponent->component->id . ']', $orderComponent->component->name, array('class'=>'form-control', 'disabled' => 'disabled')) }}
				  				{{ Form::hidden('orderComponents[' . $orderComponent->id . ']', $orderComponent->id) }}
				  				{{ Form::hidden('accessories[' . $orderComponent->component->id . ']', $orderComponent->component->accessory) }}
				  				{{ Form::hidden('cigamCodes[' . $orderComponent->component->id . ']', $orderComponent->component->code) }}
				  				{{ Form::hidden('amounts[' . $orderComponent->component->id . ']', ($orderComponent->amount == null) ? 1 : $orderComponent->amount) }}
				  				{{ Form::hidden('components[' . $orderComponent->component->id . ']', $orderComponent->component->name) }}
				  				{{ Form::hidden('disabledComponents[' . $orderComponent->component->id . ']', $orderComponent->component->name) }}
							</div>
							<div class="col-sm-3 col-component">
								{{ Form::hidden('costCentres[' . $orderComponent->component->id . ']', join($orderComponent->costsCentres->lists('id'), ','), array('data-disabled' => 'true')) }}
								<button type="button" class="btn btn-danger btn-sm btnRemoveComponent" data-toggle="tooltip" data-placement="top" title="Remover Componente">
									<span class="glyphicon glyphicon-trash"></span>
								</button>
							</div>
						</div>
					@endforeach
					
					@if (!empty($oldComponents))
						@foreach ($oldComponents as $key => $component)
							@if (!isset($oldDisabledComponents[$key]))
								<div class="form-group component-group">
									<div class="col-sm-2" style="padding-right: 3px;">
										<select name="accessories[]" class="form-control cboOperationType">
											<option value="0"@if ($oldAccessories[$key] == 0) selected@endif>Operação</option>
											<option value="1"@if ($oldAccessories[$key] == 1) selected@endif>Insumo</option>
										</select>
									</div>
									<div class="col-sm-2 col-component">
										<input type="text" class="form-control txtCigamCode" name="cigamCodes[]" value="{{ $oldCigamCodes[$key] }}" required="required" autocomplete="off"@if ($oldAccessories[$key] == 1) readonly="readonly"@endif>
									</div>
									<div class="col-sm-1 col-component">
						    			<input type="text" class="form-control" name="amounts[]" value="{{ $oldAmounts[$key] }}" autocomplete="off" required="required">
						  			</div>
									<div class="col-sm-4 col-component">
										<input type="text" class="form-control ui-autocomplete-input" name="components[]" value="{{ $component }}" autocomplete="off" required="required">
									</div>
									<div class="col-sm-3 col-component">
										<input type="hidden" name="costCentres[]" value="{{ $oldCostCentres[$key] }}">
										<button type="button" class="btn btn-danger btn-sm btnRemoveComponent" data-toggle="tooltip" data-placement="top" title="Remover Componente">
											<span class="glyphicon glyphicon-trash"></span>
										</button>
									</div>
								</div>
							@endif
						@endforeach
					@endif
					
            		<div class="form-group">
						<div class="col-sm-9">
							<button id="btnAddComponent" type="button" class="btn btn-primary">Adicionar componente</button>
						</div>
					</div>
            	</div>
            </div>
      	</div>
      
      	{{ Form::submit('Atualizar Pedido', array('id' => 'btn-save-order', 'class'=>'btn btn-primary', 'data-loading-text' => 'Aguarde...'))}}
      	{{ HTML::link('order', 'Cancelar', array('class' => 'btn btn-default', 'style'=>'float: right')) }}
      	{{ Form::button('Excluir Pedido', array('class' => 'btn btn-danger', 'data-toggle' => 'modal', 'data-target' => '#deleteDialog', 'style'=>'float: right; margin-right: 5px;')) }}
 	{{ Form::close() }}
 </div>
 <div class="modal" id="deleteDialog" tabindex="-1" role="dialog" aria-labelledby="deleteDialog" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title" id="myModalLabel">Confirmação de exclusão</h4>
			</div>
			<div class="modal-body">
				<p>Este pedido será apagado permanentemente.</p>
				<br/>
				@if (!$canEditProductionStart)
				<p style="color: red"><b>IMPORTANTE:</b> Este pedido recebeu informações sobre o seu andamento e por isso apagá-lo é desaconselhável!</p>
				<br/>				
				@endif 
       			<b>Atenção!</b> Esta operação não poderá ser desfeita, deseja prosseguir?
			</div>
			<div class="modal-footer">
				{{ Form::button('Fechar', array('class' => 'btn btn-default', 'data-dismiss' => 'modal' )) }}					
				{{ HTML::link('order/' . $order->id, 'Confirmar exclusão', array('class' => 'btn btn-danger', 'data-method' => 'delete', 'rel' => 'nofollow')) }}
			</div>
		</div>
	</div>
</div>
 <script type="text/javascript">
	var allCostsCentres = {{ $costsCentres }};
	var canEditProductionStart = {{ ($canEditProductionStart) ? 'true' : 'false' }};
	
	$(document).ready(function() {
		$("input[name^='costCentres']").costsCentres({ costsCentres: allCostsCentres });

		$('#customerId').select2({
			allowClear: true,
			placeholder: 'Selecione o cliente'
		});

		if (canEditProductionStart) {
			$('#orderDate').datepicker('option', 'onClose', function() {
				var $orderDate = $('#orderDate'),
					orderDateArray = $orderDate.val().split('/'),
					$productionStart = $('#productionStartDate');
				
					if ($orderDate.val() !== '' && $orderDate.val() !== '__/__/____' && $productionStart.val() === '') {
						var orderDate = $orderDate.datepicker('getDate'),
							currentDate = new Date();
			
						currentDate.setHours(0);
						currentDate.setMinutes(0);
						currentDate.setSeconds(0);
						currentDate.setMilliseconds(0);
			
						if (orderDate.getTime() < currentDate.getTime()) {
							$productionStart.datepicker('setDate', currentDate);
						} else {
							$productionStart.datepicker('setDate', orderDate);
						}
					}
				}
			);
		}
	});
</script>